package com.example.broadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private MyReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.申请权限
        //1.1 配置文件 AndroidManifest.xml
        //1.2 动态申请
        requestPermissions(new String[] {Manifest.permission.READ_SMS,
                Manifest.permission.RECEIVE_SMS}, 1);

        receiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        String action = "android.provider.Telephony.SMS_RECEIVER";
        intentFilter.addAction(action);
        //动态注册
        registerReceiver(receiver,intentFilter);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permission,
                                          int[] grantResults){
        super.onRequestPermissionsResult(requestCode,permission,grantResults);
        if(requestCode == 1){
            for(int i = 0; i < permission.length; i++){
                if(grantResults[i] == PackageManager.PERMISSION_GRANTED){

                }else{
                    Toast.makeText(this,"permission" + permission[i] + "requser failed", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}